package com.apes.hr.salary.rewardPunish.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.jpa.spec.JpaDsl;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.DateUtil;
import com.apes.hr.base.model.Employee;
import com.apes.hr.base.model.WorkPost;
import com.apes.hr.salary.rewardPunish.repository.TeamRewardPunishPreRepository;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.frame.model.Branch;
import com.apes.scm.masterdata.party.repository.PersonRepository;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 功能：集体奖惩记录单明细分配
 * @author linwz
 * @create 2019-02-27 11:41
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = TeamRewardPunishDistribution.CONST_TABLE_NAME)
public class TeamRewardPunishDistribution extends Model {
    public static final String CONST_TABLE_NAME = "hr_reward_punish_distribution";

    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "集体奖惩记录明细分配")
    @Comment("奖惩明细分配内码")
    private long id;   //奖惩明细分配内码

    @ManyToOne(optional = false)
    @JoinColumn(name = "Team_reward_punish_pre_id")
    @Comment("集体奖惩记录单号")
    private TeamRewardPunishPre teamRewardPunishPre;//集体奖惩记录单号

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "work_branch_id", referencedColumnName = "id")
    @Comment("工作部门")
    private Branch workBranch;   //工作部门

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "Employee_id", referencedColumnName = "ID")
    @Comment("员工")
    private Employee employee;   //员工

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "work_post_id", referencedColumnName = "id")
    @Comment("工作岗位")
    private WorkPost workPost;   //工作岗位

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "on_duty_state", referencedColumnName = "id")
    @Comment("在职状态")
    private Enumeration onDutyState; //在职状态(onDutyState)

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "transfer_on")
    @Comment("调至日期")
    private Date transferOn;//调至日期

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "transfer_out")
    @Comment("调离日期")
    private Date transferOut;//调离日期

    @Column(name = "amt", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("金额")
    private double amt = 0;    //金额

    @Column(name = "no_penalty", nullable = false, columnDefinition = "number(1,0) default 0")
    @Comment("是否免罚")
    private boolean noPenalty; //是否免罚

    @Column(name = "reprieve", nullable = false, columnDefinition = "number(1,0) default 0")
    @Comment("暂缓执行")
    private boolean reprieve;//暂缓执行

    @Column(name = "emp_value_total")
    @Comment("分值合计")
    private double empValueTotal = 0;   //员工已发文的连续12个月行政奖罚分合计

    @Column(name = "cash_date")
    @Comment("兑现日期")
    private Date cashDate; //兑现日期

    public Date getCashDate() {
        if (teamRewardPunishPre == null) return null;
        if (teamRewardPunishPre.getTeamRewardPunish().getCashDate() == null) return null;
        return teamRewardPunishPre.getTeamRewardPunish().getCashDate();
    }
}
